﻿Imports ToolKitVB
Imports ToolKitVB.Web.UI.Javascript
<Assembly: System.Web.UI.WebResource("ToolKitVB.TextBox.js", "text/javascript")> 
<System.Web.UI.ParseChildren(True, "Text"), _
System.Drawing.ToolboxBitmap(GetType(ToolKitTextBox), _
 "Web.UI.WebControls.TextBox.ico")> _
Public Class ToolKitTextBox
    Inherits WebControls.TextBox
    Implements IToolKit
    Enum AutoFormatEnum
        None
        [Integer]
        Currency
        [Decimal]
        Alphanumeric
        PhoneUS
        DateUS
    End Enum
    Private _AutoFormat As AutoFormatEnum = AutoFormatEnum.None
    ''' <summary>
    ''' Makes user unable to type in incorrect values.
    ''' </summary>
    ''' <remarks></remarks>
    Public Property AutoFormat() As AutoFormatEnum
        Get
            Return _AutoFormat
        End Get
        Set(ByVal value As AutoFormatEnum)
            _AutoFormat = value
        End Set
    End Property

    Private _AddToJavascript As Boolean = False
    ''' <summary>
    ''' Makes TextBox Available in AddToJavascript.
    ''' </summary>
    ''' <remarks></remarks>
    Public Property AddToJavascript() As Boolean
        Get
            Return _AddToJavascript
        End Get
        Set(ByVal value As Boolean)
            _AddToJavascript = value
        End Set
    End Property
    Private _OnClientKeyUp As String = ""
    Public Property OnClientKeyUp() As String
        Get
            Return _OnClientKeyUp
        End Get
        Set(ByVal value As String)
            _OnClientKeyUp = value
        End Set
    End Property
    Private _OnClientKeyDown As String = ""
    Public Property OnClientKeyDown() As String
        Get
            Return _OnClientKeyDown
        End Get
        Set(ByVal value As String)
            _OnClientKeyDown = value
        End Set
    End Property
    Private _OnClientFocus As String = ""
    Public Property OnClientFocus() As String
        Get
            Return _OnClientFocus
        End Get
        Set(ByVal value As String)
            _OnClientFocus = value
        End Set
    End Property
    Private _OnClientBlur As String = ""
    Public Property OnClientBlur() As String
        Get
            Return _OnClientBlur
        End Get
        Set(ByVal value As String)
            _OnClientBlur = value
        End Set
    End Property
    Private _OnClientChange As String = ""
    Public Property OnClientChange() As String
        Get
            Return _OnClientChange
        End Get
        Set(ByVal value As String)
            _OnClientChange = value
        End Set
    End Property
    Public Overrides Property Text() As String
        Get
            Try
                If _AutoFormat = AutoFormatEnum.Currency Then
                    If MyBase.Text = "" Then
                        MyBase.Text = "0.00"
                    Else
                        MyBase.Text = CType(MyBase.Text.Replace("$", "").Replace(",", ""), Decimal).ToString
                    End If
                ElseIf _AutoFormat = AutoFormatEnum.PhoneUS Then
                    Dim VerifyDate As DateTime
                    If DateTime.TryParse(MyBase.Text, VerifyDate) Then
                        MyBase.Text = VerifyDate.ToString()
                    Else
                        Throw New Errors.ToolKitClientException("Invalid Date")
                    End If
                ElseIf _AutoFormat = AutoFormatEnum.Alphanumeric Then
                    Dim AlphanumericChars As String = System.Text.RegularExpressions.Regex.Replace(MyBase.Text, "[^\da-z-]", "")
                    Dim Chars As New List(Of Char)(AlphanumericChars.ToCharArray)
                    MyBase.Text = String.Concat(Chars.ToArray)
                ElseIf _AutoFormat = AutoFormatEnum.PhoneUS Then
                    Dim numbers As String = System.Text.RegularExpressions.Regex.Replace(MyBase.Text, "[^\d]", "")
                    If numbers.Length = 11 AndAlso numbers.Substring(0, 1) = "1" Then numbers = numbers.Substring(1, 10)
                    If numbers.Length = 10 Then
                        Dim cnumbers As New List(Of Char)(numbers.ToCharArray)
                        cnumbers.Insert(0, "("c)
                        cnumbers.Insert(4, ")"c)
                        cnumbers.Insert(5, " "c)
                        cnumbers.Insert(9, "-"c)
                        MyBase.Text = String.Concat(cnumbers.ToArray)
                    ElseIf numbers.Length = 0 Then
                        MyBase.Text = ""
                    Else
                        Throw New Errors.ToolKitClientException("Invalid Phone Number")
                    End If
                ElseIf _AutoFormat = AutoFormatEnum.Decimal Then
                    If MyBase.Text = "" Then MyBase.Text = "0"
                    MyBase.Text = CType(MyBase.Text, Decimal).ToString
                ElseIf _AutoFormat = AutoFormatEnum.Integer Then
                    If MyBase.Text = "" Then MyBase.Text = "0"
                    MyBase.Text = CType(MyBase.Text, Int64).ToString
                End If
            Catch ex As Exception
                Errors.ToolKitUnexpectedError.Report(ex, "ToolKitTextBox", "Text")
            End Try
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        If _AutoFormat <> AutoFormatEnum.None Then
            If Not TypeOf Me.Page Is Web.UI.ToolKitPage Then
                Throw New Exception("Web.UI.WebControls.TextBox requires a use of Web.UI.Page")
            End If
            Me.ToolKitPage.Javascript.RegisterScriptSrc(Me.GetType, "ToolKitVB.TextBox.js")
        End If
        MyBase.OnLoad(e)
    End Sub
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        If _AddToJavascript Then
            Me.ToolKitPage.Javascript.RegisterControl(Me)
        End If
        MyBase.OnPreRender(e)
        If _AutoFormat = AutoFormatEnum.Decimal Then
            _OnClientKeyUp &= "FormatDecimalInput(this);"
        ElseIf _AutoFormat = AutoFormatEnum.Alphanumeric Then
            _OnClientKeyUp &= "FormatAlphanumericInput(this);"
        ElseIf _AutoFormat = AutoFormatEnum.PhoneUS Then
            _OnClientKeyUp &= "FormatPhoneUsInput(this);"
        ElseIf _AutoFormat = AutoFormatEnum.Currency Then
            _OnClientBlur &= "FormatCurrencyInput(this);"
            _OnClientKeyUp &= "FormatDecimalInput(this);"
            ToolKitPage.Javascript.AddLoadFunction("function(){FormatCurrencyInput(" & IIf(Me.AddToJavascript, Me.ID, "document.getElementById('" & Me.ClientID & "')").ToString & ");}")
        ElseIf _AutoFormat = AutoFormatEnum.Integer Then
            _OnClientKeyUp &= "FormatNumberInput(this);"
        End If
        If _OnClientKeyUp <> "" Then Me.Attributes.Add("onkeyup", ToolKitTools.WrapErrorHandler(_OnClientKeyUp, Me.ID & " OnClientKeyUp"))
        If _OnClientKeyDown <> "" Then Me.Attributes.Add("onkeydown", ToolKitTools.WrapErrorHandler(_OnClientKeyDown, Me.ID & " OnClientKeyDown"))
        If _OnClientBlur <> "" Then
            Me.Attributes.Add("onblur", IIf(Me.AutoPostBack, " var toDPB = setTimeout('__doPostBack(\'" & Me.ClientID & "\',\'\');',500);", "").ToString & ToolKitTools.WrapErrorHandler(_OnClientBlur, Me.ID & " OnClientBlur"))
        End If

        If _OnClientFocus <> "" Then Me.Attributes.Add("onfocus", ToolKitTools.WrapErrorHandler(_OnClientFocus, Me.ID & " OnClientFocus"))
        If _OnClientChange <> "" Then
            Me.Attributes.Add("onchange", IIf(Me.AutoPostBack, " var toDPB = setTimeout('__doPostBack(\'" & Me.ClientID & "\',\'\');',500);", "").ToString & ToolKitTools.WrapErrorHandler(_OnClientChange, Me.ID & " OnClientChange"))
        End If

    End Sub

    Public Property ToolKitPage() As Web.UI.ToolKitPage
        Get
            Return DirectCast(MyBase.Page, Web.UI.ToolKitPage)
        End Get
        Set(ByVal value As Web.UI.ToolKitPage)
            MyBase.Page = value
        End Set
    End Property
    Public Overrides Function ToString() As String Implements IToolKit.ToString
        Return Text
    End Function


End Class
